package org.lanplay.server;

import java.util.HashMap;

import org.apache.wicket.guice.GuiceWebApplicationFactory;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WicketFilter;

import com.google.inject.Injector;
import com.google.inject.persist.PersistFilter;
import com.google.inject.servlet.ServletModule;

public class WebModule extends ServletModule {
	private static final String APP_MAPPING_PATTERN = "/*";

	@Override
	public void configureServlets() {
		bind(WicketFilter.class).toInstance(new WicketFilter());
		bind(WebApplication.class).to(LanPlayApplication.class);

		filter(APP_MAPPING_PATTERN).through(PersistFilter.class);
		filter(APP_MAPPING_PATTERN).through(WicketFilter.class, getWicketParams());
	}

	private HashMap<String, String> getWicketParams() {
		HashMap<String, String> wicketParams = new HashMap<String, String>();
		wicketParams.put("applicationFactoryClassName", GuiceWebApplicationFactory.class.getName());
		wicketParams.put("injectorContextAttribute", Injector.class.getName());
		wicketParams.put(WicketFilter.FILTER_MAPPING_PARAM, APP_MAPPING_PATTERN);
		return wicketParams;
	}
}
